package com.game4U.Component.FlvPlayerComponent 
{
	import flash.display.Sprite;
	import flash.media.Video;
	import flash.net.FileFilter;
	import flash.net.URLRequest;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import flash.media.SoundTransform;
	import flash.events.*;
	//import flash.filesystem.File;
	
	/**
	 * ...
	 * @author Feng
	 */
	public class FlvPlayer extends Sprite
	{
		private var _width:Number = 640;
        private var _height:Number = 480;
		private var myVideo:Video;
		private var soundControl:SoundTransform = new SoundTransform();  //控制声音
		
		private var connection:NetConnection;
        private var stream:NetStream;
		
		private var videoURL:String = ""; //播放的视频地址
		
		public var AutoPlay:Boolean = true;
		private var curTime:Number;           //播放当前时间
		public var playPercent:Number = 0;           //播放进度
        private var streamPrecent:Number = 0;          //流加载进度
		
		private var videoObj:Object = new Object;  //监听事件
		public var IsPlay:Boolean = false;
		public function FlvPlayer(w:Number = 640, h:Number = 480, autoPlay:Boolean = true, url:String = "" ) 
		{
			this.addEventListener(Event.REMOVED_FROM_STAGE, clear);
			AutoPlay = autoPlay;
			videoURL = url;
			//var videoFile:File = File.applicationDirectory.resolvePath(url);
			//videoURL = new File(new File(videoFile.url).nativePath).url; // get a file:/// video path to mp4
			trace(w, h);
			SetSize(w, h);
			addChild(myVideo);
			init();
		}
		
		private function clear(evt:Event) {
			this.removeEventListener(Event.REMOVED_FROM_STAGE, clear);
			connection.removeEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			stream.removeEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			stream.removeEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
			removeEventListener(Event.ENTER_FRAME,enterHandler);
		}
		
		private function init(){                                
			connection = new NetConnection;
			connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			
			connection.connect(null);
			
			videoObj.onMetaData = function(info:Object){
				videoObj.duration = info.duration; //视频时间长度
			}
			videoObj.onPlayStatus = function(info:Object){
				//trace("播放完毕");
				dispatchEvent(new Event("FLV_PLAY_COMPLETE_EVENT", true, true));
				Stop();
			}
			videoObj.onCuePoint = function(info:Object):void {
				trace("cuepoint: time=" + info.time + " name=" + info.name + " type=" + info.type);
			}
		}
		
		private function netStatusHandler(event:NetStatusEvent)
		{
			switch (event.info.code)
			{
					case "NetConnection.Connect.Success" :
							connectStream();
							break;

					case "NetStream.Play.StreamNotFound" :
							trace("Unable to locate video: " + videoURL);
							break;
			}
		}
		
		private function connectStream()
		{
			stream= new NetStream(connection);                        
			stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);

			//videoURL =  "flv/langjiu.flv";
			myVideo.attachNetStream(stream);
			if (AutoPlay) {
				IsPlay = true;
				
				stream.play(videoURL);  
			}
			      
			//调度一些事件，如获取视频时间总长度(记得视频得处理成含有onMediaData能读取视频数据的格式)
			stream.client = videoObj;   //new CustomClient;                        
			
			addEventListener(Event.ENTER_FRAME,enterHandler);
		}
		
		private function asyncErrorHandler(event:AsyncErrorEvent):void{
				// ignore AsyncErrorEvent events.
		}
		
		private function enterHandler(e:Event){
			playPercent = stream.time / videoObj.duration ;
			streamPrecent= stream.bytesLoaded / stream.bytesTotal ;                                 
		}
		
		
		public function SetSize(w:Number = 640,h:Number = 480) {
			_width = w;
			_height = h;
			myVideo  = new Video(_width,_height);
		}
		
		public function Play() {
			curTime = stream.time;                                        
			stream.seek(curTime);
			stream.resume();   
			
			IsPlay = true;
			//videoURL = url;
			//stream.play(videoURL);  
		}
		
		public function Pause() {
			IsPlay = false;
			stream.pause();
		}
		
		public function Stop() {
			stream.seek(0);
            stream.pause();
		}
		
	}

}